﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class PipeConnectDelAccessory : HYNoTranExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute()
		{
			string text = "删除构件";
			Transaction transaction = new Transaction(base.Doc, text);
			for (;;)
			{
				try
				{
					transaction.Start();
					Reference reference = base.Sel.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, "请选择删除的实体");
					Element element = base.Doc.GetElement(reference);
					List<Connector> list = new List<Connector>();
					FamilySymbol familySymbol = null;
					int num = 0;
					bool flag = false;
					if (element.Category.Id.IntegerValue == -2008055 || element.Category.Id.IntegerValue == -2008016)
					{
						list = this.GetConnectorBothEnd(element, ref familySymbol, ref num);
						flag = true;
					}
					base.Doc.DeleteElement(element);
					if (flag && list.Count > 1)
					{
						if (familySymbol != null && !YJKRevitTools.IsSameSizeTwoConnector(list[0], list[1]))
						{
							base.Doc.Create.NewTransitionFitting(list[0], list[1]).Symbol = familySymbol;
						}
						else
						{
							try
							{
								if (list[0].Owner is Duct)
								{
									new YJKPiping<Duct>(list[0].Owner as Duct).Combine(list[1].Owner as Duct);
								}
								else
								{
									new YJKPiping<Pipe>(list[0].Owner as Pipe).Combine(list[1].Owner as Pipe);
								}
							}
							catch (Exception)
							{
							}
						}
					}
					transaction.Commit();
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException)
				{
					transaction.RollBack();
				}
				catch
				{
					transaction.RollBack();
					return Autodesk.Revit.UI.Result.Failed;
				}
				break;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private List<Connector> GetConnectorBothEnd(Element elem, ref FamilySymbol fs, ref int connType)
		{
			List<Connector> list = new List<Connector>();
			List<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(elem);
			if (elementAllConnector == null)
			{
				return list;
			}
			if (elementAllConnector.Count >= 2)
			{
				foreach (Connector connector in elementAllConnector)
				{
					Connector connectConnector = YJKRevitTools.GetConnectConnector(connector);
					if (connectConnector != null)
					{
						if (connectConnector.Owner.Category.Id.IntegerValue == -2008044 || connectConnector.Owner.Category.Id.IntegerValue == -2008000)
						{
							list.Add(connectConnector);
						}
						else
						{
							Element owner = connectConnector.Owner;
							fs = (owner as FamilyInstance).Symbol;
							foreach (Connector connector2 in YJKRevitTools.GetElementAllConnector(owner))
							{
								if (!connector2.Origin.IsAlmostEqualTo(connector.Origin) && connector2.Owner.Id != connector.Owner.Id)
								{
									Connector connectConnector2 = YJKRevitTools.GetConnectConnector(connector2);
									if (connectConnector2 != null)
									{
										connectConnector2.DisconnectFrom(connector2);
										list.Add(connectConnector2);
										break;
									}
								}
							}
							base.Doc.DeleteElement(owner);
						}
					}
				}
			}
			return list;
		}
	}
}
